package com.woniuxy.ams.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.ams.entity.*;
import com.woniuxy.ams.service.ExamineListService;
import com.woniuxy.ams.service.SelfAppraisalService;
import com.woniuxy.ams.service.UserService;
import com.woniuxy.ams.utils.MimeTypeUtils;
import com.woniuxy.ams.utils.QNUtils;
import com.woniuxy.ams.utils.SystemUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author susunxi
 * @version 1.0
 * @date 2021/12/14 17:26
 */
@Configuration
@RestController
@RequestMapping("selfAppraisal")
public class SelfAppraisalController {

    /**
     * 读取配置文件
     */
    @Value("${qiniudeploy.access-key}")
    String accessKey;

    @Value("${qiniudeploy.secret-key}")
    String secretKey;

    @Value("${qiniudeploy.bucket-name}")
    String bucketName;

    @Value("${qiniudeploy.domain}")
    String domain;

    @Value("${web.upload-path}")
    String filePath;


    SelfAppraisalService selfAppraisalService;
    ExamineListService examineListService;
    UserService userService;

    @Autowired
    public void setSelfAppraisalService(SelfAppraisalService selfAppraisalService) {
        this.selfAppraisalService = selfAppraisalService;
    }

    @Autowired
    public void setExamineListService(ExamineListService examineListService) {
        this.examineListService = examineListService;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    /**
     * 获取当前时间
     *
     * @return 返回当成时间String类型
     */
    private static String getNowDate() {
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss=");
        return localDateTime.format(dtf);
    }


    /**
     * 查询普通用户展示的信息
     * @param pageRequest 分页信息
     * @return 返回所以信息列表
     */
    @RequestMapping("showAllData")
    public ResultEntity<?> testFind(@RequestBody PageRequest<User> pageRequest) {

        User user = SystemUtils.getLoginUser();
        User data = pageRequest.getData();
        data.setId(user.getId());


        PageHelper.startPage(pageRequest.getCurrentPage(),pageRequest.getLimit());
        List<SelfInfoShowData> selfInfoShowData = selfAppraisalService.getAllAssessed(data);
        for (SelfInfoShowData selfInfoShowDatum : selfInfoShowData) {
            List<String> assessName1 = new ArrayList<>();
            ArrayList<String> names = new ArrayList<>();
            //中间表id
            for (Integer integer : selfInfoShowDatum.getCheckInfoId()) {
                String assessName = selfAppraisalService.findUserAssessName(integer);
                assessName1.add(assessName);
                List<String> checkInfoName = selfAppraisalService.findCheckInfoName(integer);
                names.addAll(checkInfoName);
            }

            selfInfoShowDatum.setCheckInfoName(names);
            selfInfoShowDatum.setAssessName(assessName1);
        }

        PageInfo<SelfInfoShowData> selfInfoShowDataPageInfo = new PageInfo<>(selfInfoShowData);
        return new ResultEntity<>(200, "查询成功", selfInfoShowDataPageInfo);
    }


    /**
     * 文件上传和自评
     * @param file           文件
     * @param selfEvaluation 用户自拍信息
     * @param eid            考核项id
     * @return 返回影响行数
     */
    @PostMapping("fileUpload")
    public ResultEntity<?> userSelfie(@RequestParam("file") MultipartFile file,
                                      @RequestParam("selfEvaluation") String selfEvaluation,
                                      @RequestParam("eid") Integer eid) {

        User loginUser = SystemUtils.getLoginUser();

        if (eid == null || selfEvaluation.equals("")) {
            return new ResultEntity<>(200, "自评不能为空", 2);
        }

        String filePath = fileUpload(file);
        int upStatus = selfAppraisalService.addSelfieInfo(filePath, selfEvaluation, eid, loginUser.getId());
        if (upStatus > 0) {
            ExamineList examineList = new ExamineList();
            examineList.setStatus("待审核");
            examineList.setSelfReviewStatus("是");
            examineList.setId(eid);
            examineListService.updateExamineList(examineList);
        }
        return new ResultEntity<>(200, "插入成功", upStatus);
    }

    /**
     * 文件上传
     *
     * @param file 文件
     * @return 返回外链下载
     */
    private String fileUpload(MultipartFile file) {
        String fileName = null;
        try {
            InputStream byteInputStream = file.getInputStream();
            String type = MimeTypeUtils.getType(file.getOriginalFilename());
            String finalFileName = getNowDate() + file.getOriginalFilename();
            fileName = QNUtils.uploadFiles(accessKey, secretKey, bucketName,
                    byteInputStream, type, finalFileName, domain);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileName;
    }

    /**
     * 用户详情查看
     * @param selfInfoShowData 前端参数
     * @return 返回详细信息
     */
    @RequestMapping("showDetails")
    public ResultEntity<?> showDetails(@RequestBody  SelfInfoShowData selfInfoShowData){
        List<SelfInfoShowData> showDetails=selfAppraisalService.showDetails(selfInfoShowData);
        return new ResultEntity<>(200,"success",showDetails);
    }

}
